from kivy.clock import Clock
from kivy.core.window import Window
from kivy.uix.button import Button


class MenuUIMix:
    menu_default_color = 108 / 255, 108 / 255, 108 / 255, 1
    menu_default_background_color = 8 / 255, 9 / 255, 13 / 255, 1
    menu_down_default_color = 201 / 255, 201 / 255, 201 / 255, 1
    menu_down_default_background_color = 69 / 255, 75 / 255, 84 / 255, 1


class MenuButton(Button, MenuUIMix):

    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        Window.bind(mouse_pos=self.on_mouse_pos)
        self.color = self.menu_default_color
        self.background_color = self.menu_default_background_color
        self.background_normal = ''
        self.background_down = ''

    def on_mouse_pos(self, widget, pos):
        _ = widget
        if not self.get_root_window():
            return
        if self.collide_point(*pos):
            Clock.schedule_once(lambda dt: self.mouse_enter())
        else:
            Clock.schedule_once(lambda dt: self.mouse_leave())

    def mouse_enter(self):
        self.set_down_default_color()

    def mouse_leave(self):
        self.set_default_color()

    def set_default_color(self):
        self.color = self.menu_default_color
        self.background_color = self.menu_default_background_color

    def set_down_default_color(self):
        self.color = self.menu_down_default_color
        self.background_color = self.menu_down_default_background_color
